####################################################################
#***2019 analysis
## Packages
# To install and open the R packages that you need for this code. 
need <- c('tidyverse','readstata13','lfe','glue','rdrobust', 'stargazer','arm', 'broom', 'ggplot2', 'dotwhisker', 'gridExtra', 'rdd', 'rddapp', 'rddensity')
have <- need %in% rownames(installed.packages()) 
if(any(!have)) install.packages(need[!have]) 
invisible(lapply(need, library, character.only=T)) 

# Change path to whereever you place the models
# To set up the working directory. 
script_folder = dirname(rstudioapi::getSourceEditorContext()$path)
setwd(glue('{script_folder}'))
rm(list = ls())
setwd("../")

#load in full dataset
load("5prepdata/RD_bonica_bowen_merged.RData")

# ## load in RD data for estimation sample for appendix later
# load("5prepdata/final_rd_data.RData")

# 
rd.data<-rd.data[order(rd.data$year,rd.data$state,rd.data$chamber,rd.data$district_id, rd.data$victory_marg),]



# This generates the p-value presented on page 10

`Election margin`<-rd.data$victory_marg
densitytest<-dc_test(`Election margin`, 0, plot = TRUE, ext.out = FALSE, htest = FALSE, level = 0.95,
                     digits = max(3, getOption("digits") - 3), timeout = 30)

pdf("7tex/manuscript/tables/sourcefiles/Figure 1.pdf", width=10, height=8)
densitytest<-dc_test(`Election margin`, 0, plot = TRUE, ext.out = FALSE, htest = FALSE, level = 0.95,
        digits = max(3, getOption("digits") - 3), timeout = 30)
dev.off()
